package dav.mobile.garuda.states;

import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actors.Button;
import com.badlogic.gdx.scenes.scene2d.actors.Image;
import com.badlogic.gdx.scenes.scene2d.actors.Button.ClickListener;

import dav.mobile.garuda.components.Constants;
import dav.mobile.garuda.components.Profile;
import dav.mobile.garuda.components.ResourceManager;
import dav.mobile.garuda.components.SoundManager;

public class StateMap extends State implements InputProcessor {
	//Enum
	public enum MapMode {QUEST, FREE};
	
	//Attribute
	MapMode m_Mode;
	
	//Map Stage
	Stage m_Stage;
	
	Image m_Background;
	Button m_SulawesiButton;
	Button m_SumateraButton;
	Button m_JawaButton;
	Button m_KalimantanButton;
	
	//Information
	TextureRegion 	m_Info;
	TextureRegion 	m_InfoImage;
	int			 	m_Selected;
	
	public StateMap(MapMode m) {
		//Set Mode
		m_Mode = m;
		if (m_Mode==MapMode.FREE) Constants.MAP_PLACES_X = 175;
		else Constants.MAP_PLACES_X = 120;
		
		//Create stage
		m_Stage = new Stage(Constants.GAME_WIDTH, Constants.GAME_HEIGHT, true);
		
		//Load background
		m_Background = new Image("Map Background", ResourceManager.m_MapBackground);
		m_Stage.addActor(m_Background);
		
		m_Info = null;
		m_InfoImage = null;
		m_Selected = 0;
		
		//Load all enabled map
		HashMap<Integer, Boolean> m_Places = Profile.getInstance().getPlaces();
	
		m_SulawesiButton = null;
		m_SumateraButton = null;
		m_JawaButton = null;
		m_KalimantanButton = null;
		
		//Create buttons and info if exists
		for (int i=0;i<Constants.GAME_PLACE_NUM;i++) {
			if (m_Places.get(i)) {
				//Initialize button
				Button m_Button = null;
				
				//Set attribute accordingly
				if (i==Constants.GAME_PLACE_BUNAKEN) {
					m_Button = new Button("Sulawesi Button", ResourceManager.m_SulawesiButton);
					m_Button.x = 240;
					m_Button.y = 160;
					
					m_Button.clickListener = new ClickListener() {
						@Override
						public void clicked(Button arg) {
							m_Info = ResourceManager.m_SulawesiInfo;
							m_InfoImage = ResourceManager.m_Sulawesi;
							m_Selected = Constants.GAME_PLACE_BUNAKEN;
							
							SoundManager.PlaySFX(SoundManager.m_MenuSound);
						}
					};	
					m_SulawesiButton = m_Button;
				} else if (i==Constants.GAME_PLACE_PRAMBANAN) {
					m_Button = new Button("Jawa Button", ResourceManager.m_JawaButton);
					m_Button.x = 120;
					m_Button.y = 100;
					
					m_Button.clickListener = new ClickListener() {
						@Override
						public void clicked(Button arg) {
							m_Info = ResourceManager.m_JawaInfo;
							m_InfoImage = ResourceManager.m_Jawa;
							m_Selected = Constants.GAME_PLACE_PRAMBANAN;
							
							SoundManager.PlaySFX(SoundManager.m_MenuSound);
						}
					};
					m_JawaButton = m_Button;
				} else if (i==Constants.GAME_PLACE_TOBA) {
					m_Button = new Button("Sumatera Button", ResourceManager.m_SumateraButton);
					m_Button.x = 20;
					m_Button.y = 200;
					
					m_Button.clickListener = new ClickListener() {
						@Override
						public void clicked(Button arg) {
							m_Info = ResourceManager.m_SumateraInfo;
							m_InfoImage = ResourceManager.m_Sumatera;
							m_Selected = Constants.GAME_PLACE_TOBA;
							
							SoundManager.PlaySFX(SoundManager.m_MenuSound);
						}
					};
					m_SumateraButton = m_Button;
				} else if (i==Constants.GAME_PLACE_BORNEO) {
					m_Button = new Button("Kalimantan Button", ResourceManager.m_KalimantanButton);
					m_Button.x = 160;
					m_Button.y = 180;
					
					m_Button.clickListener = new ClickListener() {
						@Override
						public void clicked(Button arg) {
							m_Info = ResourceManager.m_KalimantanInfo;
							m_InfoImage = ResourceManager.m_Kalimantan;
							m_Selected = Constants.GAME_PLACE_BORNEO;
							
							SoundManager.PlaySFX(SoundManager.m_MenuSound);
						}
					};
					m_KalimantanButton = m_Button;
				} 
				
				//Add to stage
				if (m_Button!=null) m_Stage.addActor(m_Button);
			}
		}
		
		//Set input processor
		Gdx.input.setInputProcessor(this);
	}
	
	@Override
	public void Update() {
		m_Stage.act(Gdx.graphics.getDeltaTime());
	}

	@Override
	public void Render() {
		m_Stage.draw();
		
		m_SpriteBatch.begin();
		if (m_Info!=null&&m_Mode==MapMode.QUEST) m_SpriteBatch.draw(m_Info, Constants.MAP_INFO_X, Constants.MAP_INFO_Y);
		if (m_InfoImage!=null) m_SpriteBatch.draw(m_InfoImage, Constants.MAP_PLACES_X, Constants.MAP_PLACES_Y);	
		m_SpriteBatch.end();
	}

	@Override
	public boolean keyDown(int keycode) {
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		m_Stage.touchDown(x, y, pointer, button);
		
		if (m_InfoImage!=null) {
			int yMax = Constants.GAME_HEIGHT - Constants.MAP_PLACES_Y;
			int yMin = Constants.GAME_HEIGHT - Constants.MAP_PLACES_Y - m_InfoImage.getRegionHeight();
			
			//if button hitted
			if (x>Constants.MAP_PLACES_X&&x<Constants.MAP_PLACES_X+m_InfoImage.getRegionWidth()&&
				y>yMin&&y<yMax) {
				SoundManager.PlaySFX(SoundManager.m_MenuSound);
				
				//go to scene in place
				if (m_Mode==MapMode.QUEST) {
					if (m_Selected==Constants.GAME_PLACE_BORNEO) { SetState(new StateScene("borneo-1")); }
					else if (m_Selected==Constants.GAME_PLACE_BUNAKEN) { SetState(new StateScene("bunaken-1")); }
					else if (m_Selected==Constants.GAME_PLACE_PRAMBANAN) { SetState(new StateScene("prambanan-1")); }
					else if (m_Selected==Constants.GAME_PLACE_TOBA) { SetState(new StateScene("toba-1")); }
				} else if (m_Mode==MapMode.FREE) {
					
				}
			}
		}
		
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		m_Stage.touchUp(x, y, pointer, button);
		
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		m_Stage.touchDragged(x, y, pointer);
		
		return false;
	}

	@Override
	public boolean touchMoved(int x, int y) {
		
		return false;
	}

	@Override
	public boolean scrolled(int amount) {

		return false;
	}
	
	@Override
	public void Dispose() {
		super.Dispose();
		
		m_Stage.removeActor(m_Background);
		if (m_SulawesiButton!=null) m_Stage.removeActor(m_SulawesiButton);
		if (m_SumateraButton!=null) m_Stage.removeActor(m_SumateraButton);
		if (m_JawaButton!=null) m_Stage.removeActor(m_JawaButton);
		if (m_KalimantanButton!=null) m_Stage.removeActor(m_KalimantanButton);
		
		m_Stage.dispose();
	}
	
}
